package com.test;

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
 
public class HardwareMonitorExample {
    public static void main(String[] args) {
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
 
        // 获取CPU信息
        CentralProcessor processor = hal.getProcessor();
        System.out.println("CPU: " + processor.toString());
        long interrupts = hal.getProcessor().getInterrupts();

        // 获取内存信息
        GlobalMemory memory = hal.getMemory();
        System.out.println("内存: " + memory.getTotal() + " bytes = " + memory.getTotal() / 1024 / 1024 + " MB");
 
        // 打印更多硬件信息...

        // 获取总内存和可用内存
        long totalMemory = memory.getTotal();
        long availableMemory = memory.getAvailable();
        // 计算内存使用率
        double memoryUsage = (totalMemory - availableMemory) * 100.0 / totalMemory;

        System.out.println("总内存: " + totalMemory / 1024 / 1024 + " MB");
        System.out.println("可用内存: " + availableMemory / 1024 / 1024 + " MB");
        System.out.println("内存使用率: " + memoryUsage + "%");

        // 获取系统CPU使用率（最近一次更新与当前之间的平均值）
        double systemCpuLoad = processor.getSystemCpuLoad(100);
        System.out.println("系统CPU使用率: " + systemCpuLoad * 100 + "%");

        // 获取每个逻辑处理器的CPU使用率
        long[] tickCounts = processor.getSystemCpuLoadTicks();
        for (int i = 0; i < tickCounts.length; i++) {
//            double cpuLoad = tickCounts[i] / (double) (processor.getSystemUpTime() - processor.getPreviousSystemCpuLoadTick());
//            System.out.println("逻辑处理器 " + i + " CPU使用率: " + cpuLoad * 100 + "%");
        }
    }
}